<?php

namespace app\admin\model\factor;

use app\admin\model\User;
use think\Model;


class AccountAuthentication extends Model
{

    

    

    // 表名
    protected $name = 'carbon_factor_authentication';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'type_text',
    ];

    protected $type = [
        'product_image' => 'array',
        'createtime' => 'timestamp:Y-m-d H:i:s',
        'updatetime' => 'timestamp:Y-m-d H:i:s',
        'checktime' => 'timestamp:Y-m-d H:i:s'
    ];

    public function factor()
    {
        return $this->hasOne(CarbonFactor::class, 'id', 'factor_id');
    }

    public function userInfo()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    public function getChecktimeTextAttr($value, $data)
    {
        $value = $value ?: ($data['checktime'] ?? "");
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }
    
    public function getStatusList()
    {
        return ['0' => '待审','1' => '已通过','2' => '驳回'];
    }

    public function getTypeList()
    {
        return ['1' => '产品','2' => '原材料'];
    }

    public function getQuantificationMethodList()
    {
        return ['1' => 'ISO 14067:2018 温室气体 产品碳足迹 量化要求和指南','2' => '产品碳足迹量化要求和指南 电动自行车 电动摩托车'];
    }

    public function getQuantificationMethodTextAttr($value, $data)
    {
        $value = $value ?: ($data['quantification_method'] ?? '');
        return $this->getQuantificationMethodList()[$value] ?? '';
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        return $this->getStatusList()[$value] ?? '';
    }
    public function getTypeTextAttr($value, $data)
    {
        $value = $value ?: ($data['type'] ?? '');
        return $this->getTypeList()[$value] ?? '';
    }

    public function getFullAddressAttr($value,$data)
    {
        return ($data['province'] ?:'').($data['city'] ?:'').($data['area'] ?:'').($data['address'] ?: '');
    }

    public function getCertificationStampAttr($value)
    {
        return $value ? cdnurl($value) : '';
    }

    public function getReportSn(): string
    {
        $no_use = true;
        $sn = '';
        while ($no_use) {
            $sn = get_random_str(3, 14);
            if (!$this->where('report_sn', $sn)->find()) {
                $no_use = false;
            }
        }
        return $sn;
    }
}
